package com.ruoyi.asset.utils;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import cn.hutool.http.HttpRequest;

import java.util.UUID;

/**
 * 赋界二手车评估请求
 *
 * @author Liu
 * @date 2025-06-23
 */
@Slf4j
@Component
public class FJRequestUtils {

    private static String URL = "/openapi/v1/truck/getAssessmentReport";

    @Autowired
    private FJSecret fjSecret;

    public String asset(String vin) {
        JSONObject res = new JSONObject();
        res.set("vincode", vin);
        String serialNo = UUID.randomUUID().toString().substring(0, 32);
        JSONObject body = new JSONObject();
        body.set("body", AesEncryptUtil.aes256Encrypt(res.toString(), fjSecret.getAesSecret(), fjSecret.getAesIv()));
        HttpRequest dongHttpPost = getDongHttpPost(URL, serialNo, res.toString());
        String body1 = dongHttpPost.body(body.toString()).execute().body();
        JSONObject jsonObject = JSONUtil.parseObj(body1);
        String body2 = jsonObject.getStr("body");
        String s = AesEncryptUtil.aes256Decrypt(body2, fjSecret.getAesSecret(), fjSecret.getAesIv());
        return s;
    }

    /**
     * 获取请求对象
     *
     * @param url
     * @return
     */
    public HttpRequest getDongHttpPost(String url,
                                       String serialNo,
                                       String body) {
        HttpRequest request = HttpRequest.post(fjSecret.getUrl() + url)
                .header("AppCode", fjSecret.getAppCode())
                .header("SerialNo", serialNo)
                .header("Content-Type", "application/json")
                .header("Signature", AesEncryptUtil.generateSignature(fjSecret.getAppSecrect(),
                        fjSecret.getAppCode() + serialNo + body));
        return request;
    }
}
